package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.CheckitemMapper;
import com.itheima.pojo.Checkitem;
import com.itheima.service.CheckitemService;
import com.itheima.service.SetmealGroupService;
import com.itheima.service.SetmealService;
import com.itheima.support.PageResult;
import com.itheima.support.QueryPageBean;
import com.itheima.utils.RedisUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

@DubboService
public class CheckitemServiceImpl extends ServiceImpl<CheckitemMapper, Checkitem> implements CheckitemService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SetmealGroupService setmealGroupService;
    @Override
    public List<Checkitem> findItemsBygId(Long gId) {
     /*   if (RedisUtil.get("checkitem:checkgroupId:"+gId)!=null){
            return RedisUtil.get("checkitem:checkgroupId:"+gId);
        }
        RedisUtil.set("checkitem:checkgroupId:"+gId,baseMapper.findItemsBygId(gId));*/
        return baseMapper.findItemsBygId(gId);
    }

    @Override
    public Boolean saveorUpdateData(Checkitem checkitem) {
        Long[] cids = setmealGroupService.findSetmealByCid(checkitem.getId());
        for (Long cid : cids) {
            redisTemplate.delete(redisTemplate.keys("setmeal:"+cid));
        }
        return this.saveOrUpdate(checkitem);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Page<Checkitem> page = page(new Page<Checkitem>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()));
        return new PageResult(page.getTotal(),page.getRecords());

    }
}
